package cn.csg.iotplatform.module.system.service.user;


import cn.csg.iotplatform.framework.common.util.validation.ValidationUtils;
import cn.csg.iotplatform.module.common.imports.ExcelImportException;
import cn.csg.iotplatform.module.common.imports.ExcelValidate;
import cn.csg.iotplatform.module.common.util.ExcelImportUtil;
import cn.csg.iotplatform.module.system.controller.admin.user.vo.user.UserImportExcelDto;
import cn.csg.iotplatform.module.system.dal.dataobject.user.AdminUserDO;
import cn.csg.iotplatform.module.system.dal.mysql.user.AdminUserMapper;
import cn.csg.iotplatform.module.system.service.dept.DeptService;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import jodd.util.StringUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.Z
 * @description: [用户管理]用户导入 逻辑校验
 * @date 2024/3/14 16:24
 * @version: v2.0
 */
public class UserImportValidate extends ExcelValidate {

    private final AdminUserMapper userMapper;
    private final DeptService deptService;

    public UserImportValidate() {
        this.userMapper = SpringUtil.getBean(AdminUserMapper.class);
        this.deptService = SpringUtil.getBean(DeptService.class);
    }

    @Override
    protected String validateAndFormatColumn(Integer rowIndex, Integer colIndex, String columnName, String value, Map<String, String> row) throws ExcelValidateException {
        switch (columnName) {
            case "用户账号":
                if (StrUtil.isBlank(value)) throw new ExcelValidateException("不能为空");
                if(!ValidationUtils.isUserAccount(value)) throw new ExcelValidateException("账号由4-30位数字、字母及符号组成");
                AdminUserDO userByUsername = this.userMapper.selectByUsername(value);
                if (userByUsername != null) {
                    throw new ExcelValidateException("已存在");
                }
                break;
            case "用户姓名":
                if (StrUtil.isBlank(value)) throw new ExcelValidateException("不能为空");
            case "手机":
                if (StringUtil.isBlank(value)) {
                    break;
                }
                AdminUserDO userByMobile = this.userMapper.selectByMobile(value);
                if (userByMobile != null) {
                    throw new ExcelValidateException("已存在");
                }
                break;
            case "邮箱":
                if (StringUtil.isBlank(value)) {
                    break;
                }
                AdminUserDO userByEmail = this.userMapper.selectByEmail(value);
                if (userByEmail != null) {
                    throw new ExcelValidateException("已存在");
                }
                break;
            case "工号":
                if (StringUtil.isBlank(value)) {
                    break;
                }
                AdminUserDO userByUserNo = this.userMapper.selectByUserNo(value);
                if (userByUserNo != null) {
                    throw new ExcelValidateException("已存在");
                }
                break;
//            case "所属部门ID":
//                if (StringUtil.isBlank(value)) {
//                    throw new ExcelValidateException("不能为空");
//                }
//                try {
//                    this.deptService.validateDeptList(CollectionUtils.singleton(Long.valueOf(value)));
//                } catch (NumberFormatException e) {
//                    throw new ExcelValidateException("不能填非数字部门ID");
//                } catch (Exception e) {
//                    throw new ExcelValidateException(e.getMessage());
//                }
//                break;
            case "所属部门":
                if (StringUtil.isBlank(value)) {
                    throw new ExcelValidateException("不能为空");
                }
                try {
                    this.deptService.validateDeptName(value);
                } catch (Exception e) {
                    throw new ExcelValidateException(e.getMessage());
                }
                break;
            case "租户类型":
                if (StrUtil.isBlank(value)) throw new ExcelValidateException("不能为空");
                String[] isTenantArray = {"租户类型", "非租户类型"};
                List<String> isTenantList = Arrays.asList(isTenantArray);
                if (!isTenantList.contains(value)) {
                    throw new ExcelValidateException("非枚举内选项");
                }
                break;
            case "性别":
                String[] sexArray = {"男", "女", "其他"};
                List<String> sexList = Arrays.asList(sexArray);
                if (!sexList.contains(value)) {
                    throw new ExcelValidateException("非枚举内选项");
                }
                break;
            case "是否有效":
                if (StrUtil.isBlank(value)) throw new ExcelValidateException("不能为空");
                String[] isValidArray = {"有效", "无效"};
                List<String> isValidList = Arrays.asList(isValidArray);
                if (!isValidList.contains(value)) {
                    throw new ExcelValidateException("非枚举内选项");
                }
                break;
            default:
                break;
        }
        return null;
    }

    @Override
    protected void validateHead(Map<Integer, String> heard) throws ExcelImportException {
        ExcelImportUtil.checkHeaderByEntity(UserImportExcelDto.class, heard.values());
    }

}
